#ifndef __ThreadDefinesBoost_H__
#define __ThreadDefinesBoost_H__

#include "boost\thread.hpp"
#include "boost\thread\condition.hpp"

////for thread///
#define AUTO_MUTEX mutable boost::recursive_mutex AUTO_MUTEX_NAME;
#define LOCK_AUTO_MUTEX boost::recursive_mutex::scoped_lock AutoMutexLock(AUTO_MUTEX_NAME);
#define MUTEX(name) mutable boost::recursive_mutex name;
#define STATIC_MUTEX(name) static boost::recursive_mutex name;
#define STATIC_MUTEX_INSTANCE(name) boost::recursive_mutex name;
#define LOCK_MUTEX(name) boost::recursive_mutex::scoped_lock nameLock(name);
#define LOCK_MUTEX_NAMED(mutexName, lockName) boost::recursive_mutex::scoped_lock lockName(mutexName);
// like AUTO_MUTEX but mutex held by pointer
#define AUTO_SHARED_MUTEX mutable boost::recursive_mutex *AUTO_MUTEX_NAME;
#define LOCK_AUTO_SHARED_MUTEX assert(AUTO_MUTEX_NAME); boost::recursive_mutex::scoped_lock autoMutexLock(*AUTO_MUTEX_NAME);
#define NEW_AUTO_SHARED_MUTEX assert(!AUTO_MUTEX_NAME); AUTO_MUTEX_NAME = new boost::recursive_mutex();
#define DELETE_AUTO_SHARED_MUTEX assert(AUTO_MUTEX_NAME); delete AUTO_MUTEX_NAME;
#define COPY_AUTO_SHARED_MUTEX(from) assert(!AUTO_MUTEX_NAME); AUTO_MUTEX_NAME = from;
#define SET_AUTO_SHARED_MUTEX_NULL AUTO_MUTEX_NAME = 0;
#define MUTEX_CONDITIONAL(mutex) if (mutex)
#define THREAD_SYNCHRONISER(sync) boost::condition sync;
#define THREAD_WAIT(sync, mutex, lock) sync.wait(lock);
#define THREAD_NOTIFY_ONE(sync) sync.notify_one(); 
#define THREAD_NOTIFY_ALL(sync) sync.notify_all(); 
// Read-write mutex
#define RW_MUTEX(name) mutable boost::shared_mutex name
#define LOCK_RW_MUTEX_READ(name) boost::shared_lock<boost::shared_mutex> nameLock(name)
#define LOCK_RW_MUTEX_WRITE(name) boost::unique_lock<boost::shared_mutex> nameLock(name)
// Thread-local pointer
#define THREAD_POINTER(T, var) boost::thread_specific_ptr<T> var
#define THREAD_POINTER_INIT(var) var(&deletePtr)
#define THREAD_POINTER_VAR(T, var) boost::thread_specific_ptr<T> var (&deletePtr<T>)
#define THREAD_POINTER_SET(var, expr) var.reset(expr)
#define THREAD_POINTER_GET(var) var.get()
#define THREAD_POINTER_DELETE(var) var.reset(0)
// Thread objects and related functions
#define THREAD_TYPE boost::thread
#define THREAD_CREATE(name, workerFunc) boost::thread* name = new boost::thread(workerFunc);
#define THREAD_DESTROY(name) DELETE_T(name, thread, MEMCATEGORY_GENERAL)
#define THREAD_HARDWARE_CONCURRENCY boost::thread::hardware_concurrency()
#define THREAD_CURRENT_ID boost::this_thread::get_id()
#define THREAD_WORKER_INHERIT
// Utility
#define THREAD_SLEEP(ms) boost::this_thread::sleep(boost::posix_time::millisec(ms));

#endif